function Window(id, name, mgr, icon, url) {
	this.id = id;
	this.name = name;
	this.gid = null;
	this.gname= null;
	
	this.mgr = mgr;//windows管理器, taskbar或者一个其他windows管理器,必须有windows这个数组变量
	this.doc = document.body;
	
	//create
	this.winObj = document.createElement("div");
	this.winObj.id = this.id;
	this.winObj.className = "window_div";
	this.doc.insertAdjacentElement("beforeEnd", this.winObj);
	
	this.titleObj = document.createElement("div");
	this.titleObj.className = "windowtitle_div_active";
	
	this.clientObj = document.createElement("div");
	this.clientObj.className = "windowclient_div";
	
	this.winObj.insertAdjacentElement("beforeEnd", this.titleObj);
	this.winObj.insertAdjacentElement("beforeEnd", this.clientObj);
	
	this.nameObj = document.createElement("span");
	this.nameObj.className = "windowtitle";
	this.nameObj.innerHTML = this.name;
	
	this.minBtObj = document.createElement("image");
	this.minBtObj.src = globeWebAppContext + "/brick/image/minimize.gif";
	this.minBtObj.className = "windowbutton";
	this.minBtObj.alt = "最小化";
	
	this.maxBtObj = document.createElement("image");
	this.maxBtObj.src = globeWebAppContext + "/brick/image/maximize.gif";
	this.maxBtObj.className = "windowbutton";
	this.maxBtObj.alt = "最大化";
	
	this.closeBtObj = document.createElement("image");
	this.closeBtObj.src = globeWebAppContext + "/brick/image/close.gif";
	this.closeBtObj.className = "windowbutton";
	this.closeBtObj.alt = "关闭";
	
	this.titleObj.insertAdjacentElement("beforeEnd", this.nameObj);
//	this.titleObj.insertAdjacentHTML("beforeEnd", "&nbsp;");
	this.titleObj.insertAdjacentElement("beforeEnd", this.minBtObj);
	this.titleObj.insertAdjacentHTML("beforeEnd", "&nbsp;");
	this.titleObj.insertAdjacentElement("beforeEnd", this.maxBtObj);
	this.titleObj.insertAdjacentHTML("beforeEnd", "&nbsp;");
	this.titleObj.insertAdjacentElement("beforeEnd", this.closeBtObj);
	
	this.iframeObj = null;
	if (url && url != null) {
		this.iframeObj = document.createElement("iframe")
		this.iframeObj.width = "100%";
		this.iframeObj.height = "100%";
		this.iframeObj.frameBorder = 0;
		this.iframeObj.name = this.id;
		this.clientObj.insertAdjacentElement("beforeEnd", this.iframeObj);
		this.iframeObj.src = url;
	}
	
	//param init
	this.top = 300;
	this.left = 10;
	this.width = 200;
	this.height = 200;
	
	this.miniWidth = 80;
	this.miniHeight = 30;
	this.buttonWidth = 60;
	this.textHeight = 25;
	this.taskbarHeight = 0;
	if (this.mgr && this.mgr.height && !isNaN(this.mgr.height)) this.taskbarHeight = this.mgr.height;
	
	
	this.minimize = false;
	this.maximize = false;
	this.active = true;
	
	this.moving = false;
	this.xOffset = 0;
	this.yOffset = 0;
	
	this.resizing = false;
	this.resizeDir = "";
	this.xPosition = 0;
	this.yPosition = 0;
	this.resizeBorder = 2;
	this.oldTop = this.top;
	this.oldLeft = this.left;
	this.oldWidth = this.width;
	this.oldHeight = this.height;
	
	
	//function
	this.mo = null;
	this.ms = null;
	this.ro = null;
	this.rs = null;
	
	//event
	var oThis = this;
	//最小化
	this.minBtObj.onclick = function () {
		if (!oThis.minimize)
			oThis.doMinimize();
		else
			oThis.doRestore();
	};
	//最大化
	this.maxBtObj.onclick = function () {
		if (!oThis.maximize)
			oThis.doMaximize();
		else
			oThis.doRestore();
	};
	//关闭
	this.closeBtObj.onclick = function () {
		oThis.close();
	}
	//屏蔽选中
	this.titleObj.onselectstart = function () {
		return false;
	}
	//开始拖动
	this.titleObj.onmousedown = function (event) {
		oThis.moveDragStart(event);
	}
	//active
	this.winObj.onmousedown = function (event) {
		oThis.doActive();
		oThis.resizeDragStart(event);
	}
	//改大小
	this.winObj.onmousemove = function (event) {
		oThis.resizeCursorSet(event);
	}
	this.winObj.onmouseout = function (event) {
		oThis.resizeCursorRestore(event);
	}
	
		
	//append to mgr
	if (this.mgr && typeof this.mgr.windows == 'object') {
		this.left = this.mgr.calLeft();
		this.top = this.mgr.calTop();
		this.mgr.append(this);
	}
	
	//lcation init
	this.refresh();
	
	//active
	this.doActive();
}
Window.prototype.show = function () {
	this.winObj.style.display = "block";
};
Window.prototype.hide = function () {
	this.winObj.style.display = "none";
};
Window.prototype.resize = function (w, h) {
	if (w) this.width = w;
	if (h) this.height = h;
	this.refresh();
};
Window.prototype.locate = function (x, y) {
	if (x) this.left = x;
	if (y) this.top = y;
	this.refresh();
};
Window.prototype.refresh = function () {
	this.winObj.style.left = this.left;
	this.winObj.style.top = this.top;
	this.winObj.style.width = this.width;
	
	this.clientObj.style.height = this.height - this.textHeight - 6;
	this.nameObj.style.width = this.width - this.buttonWidth;
}

Window.prototype.close = function () {
	this.hide();
	this.doc.removeChild(this.winObj);
	
	if (this.mgr && typeof this.mgr.groups == 'object' && this.gid) {
		this.mgr.removeTaskButton(this.gid, this.id);
	}
	
	if (this.mgr && typeof this.mgr.windows == 'object') {
		this.mgr.remove(this.id);
	}
};
Window.prototype.group = function(gid, gname) {
	this.gid = gid;
	this.gname = gname;
	
	if (this.mgr && typeof this.mgr.groups == 'object') {
		var g = this.mgr.createTaskGroup(this.gid, this.gname, this.icon);
		var b = g.createTaskButton(this.id, this.name, this.icon);
		b.window = this;
	}
}
Window.prototype.doActive = function () {
	//first inactive all
	if (this.mgr && typeof this.mgr.windows == 'object') {
		this.mgr.inactiveAll();
		this.winObj.style.zIndex = ++this.mgr.zIndex;
	}
	
	//active this one
	this.active = true;
	this.titleObj.className = "windowtitle_div_active";
	
};
Window.prototype.inActive = function () {
	this.active = false;
	this.titleObj.className = "windowtitle_div_inactive";
};
Window.prototype.doMinimize = function () {
	if (this.mgr && typeof this.mgr.groups == 'object') {
		this.winObj.style.display = "none";
	} else {
		if (this.maximize) this.refresh();
		this.clientObj.style.display = "none";
	}
	this.minimize = true;
	this.maximize = false;
};
Window.prototype.doRestore = function () {
	if (this.minimize) {
		this.minimize = false;
		if (this.mgr && typeof this.mgr.groups == 'object') {
			this.winObj.style.display = "block";
		} else {
			this.clientObj.style.display = "block";
		}
	} else if (this.maximize) {
		this.maximize = false;
		this.maxBtObj.src = globeWebAppContext + "/brick/image/maximize.gif";
		this.refresh();
	}
};
Window.prototype.doMaximize = function () {
	if (this.minimize) this.clientObj.style.display = "block";
	this.minimize = false;
	this.maximize = true;
	
	this.maxBtObj.src = globeWebAppContext + "/brick/image/restore.gif";
	
	this.winObj.style.left = 0;
	this.winObj.style.top = 0;
	this.winObj.style.width = posLib.getWidth(this.doc);
	
	this.clientObj.style.height = posLib.getHeight(this.doc) - this.textHeight - 6 - this.taskbarHeight;
	this.nameObj.style.width = posLib.getWidth(this.doc) - this.buttonWidth;
}

Window.prototype.moveDragStart = function (event) {
	
	var e = (event)?event:window.event;
	var target = (e.srcElement)?e.srcElement.tagName:e.target.tagName;
	var x, y;
	if (target == "IMAGE") return;

	// Get cursor offset from window frame.
  	if (document.all) {
		x = e.x;
		y = e.y;
	} else {
		x = e.pageX;
		y = e.pageY;
	}

	this.xOffset = this.left - x;
	this.yOffset = this.top  - y;

  	//Set document to capture mousemove and mouseup events.	
	var oThis = this;
	this.mo = function (event) {
		oThis.moveDragOn(event);
	};
	this.ms = function (event) {
		oThis.moveDragStop(event);
	};
	if (document.all) {
		document.attachEvent("onmousemove", this.mo);
		document.attachEvent("onmouseup", this.ms);
	} else {
		document.addEventListener("mousemove", this.mo, true);
		document.addEventListener("mouseup", this.ms, true);
		e.preventDefault();
	}
	this.moving = true;
	if (this.iframeObj && this.iframeObj != null) {
		this.iframeObj.style.display = "none";
	}
};

Window.prototype.moveDragOn = function (event) {
	if (!this.moving) return;

	var e = (event)?event:window.event;
	var x, y;

	//Get cursor position.
	if (document.all) {
		x = e.x;
		y = e.y;
		e.cancelBubble = true;
		e.returnValue = false;
	} else {
		x = e.pageX;
		y = e.pageY;
		e.preventDefault();
	}

	// Move window frame based on offset from cursor.
	this.locate(x + this.xOffset, y + this.yOffset);
};

Window.prototype.moveDragStop = function (event) {
	this.moving = false;
	if (this.iframeObj && this.iframeObj != null) {
		this.iframeObj.style.display = "block";
	}
	var oThis = this;
//	var mo = function (event) {
//		oThis.moveDragOn(event);
//	};
//	var ms = function (event) {
//		oThis.moveDragStop(event);
//	};
	if (document.all) {
		document.detachEvent("onmousemove", this.mo);
		document.detachEvent("onmouseup", this.ms);
	} else {
		document.removeEventListener("mousemove", this.mo, true);
		document.removeEventListener("mouseup", this.ms, true);
		event.preventDefault();
	}
	this.mo = null;
	this.ms = null;
}

Window.prototype.resizeCursorSet = function (event) {
	if (this.minimize || this.resizing) return;
	
	var e = (event)?event:window.event;
	var target = (e.srcElement)?e.srcElement:e.target;
	var x, y;
	
	if (target != this.winObj) return;
	
	if (document.all) {
		x = e.offsetX;
		y = e.offsetY;
	} else {
		x = e.layerX;
		y = e.layerY;
	}

	this.resizeDir = "";
	if (y <= this.resizeBorder)
		this.resizeDir += "n";
	else if (y >= this.height - this.resizeBorder*2)
		this.resizeDir += "s";
	if (x <= this.resizeBorder)
		this.resizeDir += "w";
	else if (x >= this.width - this.resizeBorder*2)
		this.resizeDir += "e";	
	if (this.resizeDir == "") return;
	
	if (document.all)
		document.body.style.cursor = this.resizeDir + "-resize";
	else
		this.winObj.style.cursor = this.resizeDir + "-resize";
}

Window.prototype.resizeCursorRestore = function (event) {
	if (this.resizing) return;
	if (document.all)
    	document.body.style.cursor = "";
	else
    	this.winObj.style.cursor = "";
}

Window.prototype.resizeDragStart = function (event) {
	if (this.minimize) return;
	
	var e = (event)?event:window.event;
	var target = (e.srcElement)?e.srcElement:e.target;
	if (target != this.winObj) return;
	
	// Save window frame position and current window size.
	this.oldTop = this.top;
	this.oldLeft = this.left;
	this.oldWidth = this.width;
	this.oldHeight = this.height;
	
	// Save cursor position.
  	if (document.all) {
		this.xPosition = e.x;
		this.yPosition = e.y;
	} else {
		this.xPosition = e.pageX;
		this.yPosition = e.pageY;
	}

  	//Set document to capture mousemove and mouseup events.	
	var oThis = this;
	this.ro = function (event) {
		oThis.resizeDragOn(event);
	};
	this.rs = function (event) {
		oThis.resizeDragStop(event);
	};
	if (document.all) {
		document.attachEvent("onmousemove", this.ro);
		document.attachEvent("onmouseup", this.rs);
	} else {
		document.addEventListener("mousemove", this.ro, true);
		document.addEventListener("mouseup", this.rs, true);
		e.preventDefault();
	}
	this.resizing = true;
	if (this.iframeObj && this.iframeObj != null) {
		this.iframeObj.style.display = "none";
	}
}


Window.prototype.resizeDragOn = function (event) {
	if (!this.resizing) return;
	
	var e = (event)?event:window.event;
	var north, south, east, west;
	var dx, dy;
	var w, h;
	
	//Set direction flags based on original resize direction.
	north = false;
	south = false;
	east  = false;
	west  = false;
  
	if (this.resizeDir.charAt(0) == "n") north = true;
	if (this.resizeDir.charAt(0) == "s") south = true;
	if (this.resizeDir.charAt(0) == "e" || this.resizeDir.charAt(1) == "e") east = true;
	if (this.resizeDir.charAt(0) == "w" || this.resizeDir.charAt(1) == "w") west = true;

	//Find change in cursor position.
	if (document.all) {
		dx = e.x - this.xPosition;
		dy = e.y - this.yPosition;
	} else {
		dx = e.pageX - this.xPosition;
		dy = e.pageY - this.yPosition;
	}
	
	//If resizing north or west, reverse corresponding amount.
	if (west) dx = -dx;
	if (north) dy = -dy;
	
	//Check new size.
	w = this.oldWidth  + dx;
	h = this.oldHeight + dy;
	if (w <= this.miniWidth) {
		w = this.miniWidth;
		dx = w - this.oldWidth;
	}
	if (h <= this.miniHeight) {
		h = this.miniHeight;
		dy = h - this.oldHeight;
	}
	
	//Resize the window. For IE, keep client area and frame widths in synch.
	if (east || west) this.width = w;
	if (north || south) this.height = h;
	
	// For a north or west resize, move the window.
	if (west) this.left = this.oldLeft - dx;
	if (north) this.top = this.oldTop - dy;
	
	this.refresh();
	
	if (document.all) {
    	e.cancelBubble = true;
    	e.returnValue = false;
    } else
    	e.preventDefault();
}

Window.prototype.resizeDragStop = function (event) {
	this.resizing = false;
	if (this.iframeObj && this.iframeObj != null) {
		this.iframeObj.style.display = "block";
	}
	var oThis = this;
//	var ro = function (event) {
//		oThis.resizeDragOn(event);
//	};
//	var rs = function (event) {
//		oThis.resizeDragStop(event);
//	};
	if (document.all) {
		document.detachEvent("onmousemove", this.ro);
		document.detachEvent("onmouseup", this.rs);
	} else {
		document.removeEventListener("mousemove", this.ro, true);
		document.removeEventListener("mouseup", this.rs, true);
		event.preventDefault();
	}
	this.ro = null;
	this.rs = null;
}
